#include Once "../inc/map.bi"

function load_map(file as string, byref ret_map as map) as integer

   dim as Integer f,flag = 0
   dim temp as string * 8
   Dim As UByte read_byte '1 byte
   Dim As UShort read_short ' 2 bytes
   Dim As integer read_int,read_size '4 bytes

   f = freefile()
   var ret = open( file for binary access read as #f )
   
   ' Run time error?
   if ret <> 0 then
      ? "Error opening map file: '" & file & "' Err# " & ret
      Return ret
   EndIf
   
   ' Check the header first
   Get #f,,temp
   ? temp
   
   If Not(temp = MAP_HEADER) Then
      ?"Invalid Map header!"
      Close #f
      Return MAP_ERROR_BAD_HEADER
   End If
   

   ' start the reading
   While(flag = 0)
      ' get the descriptor first
      Get #f, ,read_int
      get #f, ,read_size
      ? "read_int: " & read_int
      ? "read_size: " & read_size
      
      Select Case (read_int)
         Case MAP_NAME, MAP_DESCRIPTION:
         
            Dim As zString ptr temp = Callocate( read_size + 1 )
            Get #f, ,*cptr(byte ptr, temp), read_size
            
            if read_int = MAP_NAME then
               ret_map.name = *temp
               ? ">> Map Name : " & ret_map.name
            else
               ret_map.desc = *temp
               ? ">> Map Desc : " & ret_map.desc
            end if

            DeAllocate(temp)  
                    
         Case MAP_DATA:
            
            ' Format: MAP_DATA SIZE X_COORD Y_COORD 
            
            ? ">> Map Data : "
         
            With ret_map
            
               Get #f,,read_byte 
               .max_x = read_byte 'x coord
               Get #f,,read_byte
               .max_y = read_byte 'y coord
               ? "Map size : (" & .max_x & "," & .max_y & ")"
            
               .grid = Callocate ((.max_x * SizeOf(map_block Ptr)) + 1)
               
               For i As Integer = 1 To .max_x
                  .grid[i] = CAllocate ((.max_y * SizeOf(map_block)) + 1)  
               Next i 
           
               For y As Integer = 1 To .max_y
               
                  For x As Integer = 1 To .max_x
                     
                     ' Thus for each Map_Block we'll have
                     ' Format: TILE_ID DESCRIPTOR
            
                     Get #f,,read_byte ' the tile id
                     .grid[x][y].id = read_byte
                     Print read_byte & " " ;

                     Get #f,,read_byte ' the descriptor
               
                     Select Case (read_byte)                  
                        Case MAP_DATA_TELEPORT
                           ' Format: MAP_DATA_TELEPORT TELE_X TELE_Y RESERVED RESERVED
                           
                           Get #f,,.grid[x][y].gp_x ' teleport x
                           Get #f,,.grid[x][y].gp_y ' teleport y
                           Get #f,,read_byte 'two reserved blocks
                           Get #f,,read_byte
                           
                        Case MAP_DATA_SWITCH
                           
                           ' Pending
                           
                        Case Else
                           ' Format: MAP_DATA_NULL
                           ' Do nothing
                                                      
                     End Select
                  Next x
                  Print
               Next y
               
            End With
         Case MAP_NUM_HEROES:
            ' Format:  Map_num_heroes No_of_heroes
            
            ret_map.default_heroes = read_size
            ? ">> Map Number of Heroes : " & read_size
            
         Case MAP_TILES_GFX:
            
            '----------------- PENDING ---------------------------------
	
            '	Format: MAP_TILES_GFX	NO_OF_FILES	<LIST OF FILES>

            '  Where <LIST OF FILES> will contain individual descriptors as follows:

	         '  ENUM_MAP_TILES		SIZE		STRING
	         
            ''	The tile id	   size of path		the path to the file

            
      End Select
      
      If (Eof(f)) Then flag = 1
      
   wend
   
   Close #f
   
   Function = ret

end function
